package com.lul15834.orderfood.activity.user;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.Manager;
import com.lul15834.orderfood.activity.manager.ManagerAddFoodActivity;
import com.lul15834.orderfood.activity.manager.ManagerUpdateFood;
import com.lul15834.orderfood.bean.AddressBean;
import com.lul15834.orderfood.dao.AddressDao;
import com.lul15834.orderfood.dao.FoodDao;

import java.io.Serializable;

public class UpdateAddress extends AppCompatActivity {
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_update_address);
        //加载上个界面传过来的数据
        Toolbar toolbar=findViewById(R.id.user_update_address_back);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(UpdateAddress.this,UserAddress.class);
                startActivity(intent);
            }
        });
        Intent intent=getIntent();
        AddressBean address=(AddressBean)intent.getSerializableExtra("address");
        id=address.getsId();
        EditText name1=findViewById(R.id.user_update_address_name);
        name1.setText(address.getsUserName());
        EditText address1=findViewById(R.id.user_update_address);
        address1.setText(address.getsUserAddress());
        EditText phone1=findViewById(R.id.user_update_address_phone);
        phone1.setText(address.getsUserPhone());
        Button btn=findViewById(R.id.user_update_address_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=name1.getText().toString();
                String address=address1.getText().toString();
                String phone=phone1.getText().toString();
                if (name.isEmpty()){
                    Toast.makeText(UpdateAddress.this, "请输入收货人昵称", Toast.LENGTH_SHORT).show();
                }else if (address.isEmpty()){
                    Toast.makeText(UpdateAddress.this, "请输入收货人地址", Toast.LENGTH_SHORT).show();
                }else if ((phone.isEmpty())){
                    Toast.makeText(UpdateAddress.this, "请输入收货人联系方式", Toast.LENGTH_SHORT).show();
                }else{
                    int a=AddressDao.updateAddress(id,name,address,phone);
                    if (a==1){
                        Toast.makeText(UpdateAddress.this, "修改成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(UpdateAddress.this, "修改失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.user_address_del_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int a=item.getItemId();
        if (a==R.id.user_address_del){
            //删除，点击删除，是否确认删除
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("删除商品");
            builder.setMessage("确定删除吗!");
            builder.setCancelable(false);
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int a= AddressDao.delAddress(id);
                    if (a==1){
                        Toast.makeText(UpdateAddress.this,"删除成功",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(UpdateAddress.this, UserAddress.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(UpdateAddress.this,"删除失败",Toast.LENGTH_SHORT).show();
                    }
                    dialog.dismiss();
                }
            });
            AlertDialog alertDialog=builder.create();
            alertDialog.show();
        }
        return super.onOptionsItemSelected(item);
    }
}